package com.frank.oj.service.entity.discussion.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frank.model.entity.contest.Contest;
import com.frank.model.entity.discussion.Comment;
import com.frank.model.entity.discussion.Discussion;
import com.frank.model.entity.group.GroupMember;
import com.frank.model.entity.msg.MsgRemind;
import com.frank.oj.constant.Constants;
import com.frank.oj.mapper.CommentMapper;
import com.frank.oj.model.vo.CommentVO;
import com.frank.oj.service.entity.contest.ContestEntityService;
import com.frank.oj.service.entity.discussion.CommentEntityService;
import com.frank.oj.service.entity.discussion.DiscussionEntityService;
import com.frank.oj.service.entity.group.GroupMemberEntityService;
import com.frank.oj.service.entity.msg.MsgRemindEntityService;
import com.frank.oj.service.entity.msg.impl.ReplyEntityServiceImpl;
import com.frank.oj.service.entity.user.UserInfoEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author frank
 * @Date 2024/4/15
 */
@Service
public class CommentEntityServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentEntityService {
    @Resource
    private CommentMapper commentMapper;

    @Resource
    private DiscussionEntityService discussionEntityService;

    @Resource
    private ContestEntityService contestEntityService;

    @Resource
    private UserInfoEntityService userInfoEntityService;

    @Resource
    private MsgRemindEntityService msgRemindEntityService;

    @Resource
    private GroupMemberEntityService groupMemberEntityService;

    @Override
    public IPage<CommentVO> getCommentList(Integer limit, Integer currentPage, Long cid, Integer did, boolean isRoot, String uid) {

        Page<CommentVO> page = new Page<>();
        if (cid != null) { //比赛评论
            Contest contest = contestEntityService.getById(cid);
            boolean onlyMineAndAdmin = contest.getStatus().equals(Constants.Contest.STATUS_RUNNING.getCode()) && isRoot && !contest.getUid().equals(uid);
            if (onlyMineAndAdmin) { //比赛进行中仅超管和自己可见
                List<String> superAdminUidList = userInfoEntityService.getSuperAdminUidList();
                superAdminUidList.add(uid); //评论发布者
                superAdminUidList.add(contest.getUid()); //比赛创建人

                Long gid = contest.getGid();
                if (gid != null) {
                    LambdaQueryWrapper<GroupMember> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(GroupMember::getGid, gid).eq(GroupMember::getAuth, 5); //超管
                    List<GroupMember> groupAdminMemberList = groupMemberEntityService.list(queryWrapper);

                    groupAdminMemberList.forEach(groupMember -> {
                        superAdminUidList.add(groupMember.getUid());
                    });
                }
                return commentMapper.getCommentList(page, cid, did, true, superAdminUidList);
            }
        }
        return commentMapper.getCommentList(page, cid, did, false, null);
    }

    @Override
    public void updateCommentMsg(String recipientId, String senderId, String content, Integer discussionId, Long gid) {
        if (content.length() > 200) {
            content = content.substring(0, 200) + "...";

            MsgRemind msgRemind = new MsgRemind();
            msgRemind.setAction("Discuss")
                    .setRecipientId(recipientId)
                    .setSenderId(senderId)
                    .setSourceContent(content)
                    .setSourceId(discussionId)
                    .setSourceType("Discussion");

            if (gid != null) {
                msgRemind.setUrl("/group/" + gid + "/discussion-detail/" + discussionId);
            } else {
                msgRemind.setUrl("/discussion-detail/" + discussionId);
            }
            msgRemindEntityService.saveOrUpdate(msgRemind);
        }

    }

    @Override
    public void updateCommentLiseMsg(String recipientId, String senderId, Integer sourceId, String sourceType) {
        MsgRemind msgRemind = new MsgRemind();
        msgRemind.setAction("Like_Discuss")
                .setRecipientId(recipientId)
                .setSenderId(senderId)
                .setSourceId(sourceId)
                .setSourceType(sourceType);

        ReplyEntityServiceImpl.messageAlert(sourceId, sourceType, msgRemind, discussionEntityService, msgRemindEntityService);
    }
}
